import { createStore } from 'vuex'
import Vue from 'vue'
//import Vue from 'vue'
import {render} from "vue";

//1.安装插件
//vue.use
//2.创建对象
const moduleA = {
  state: {
    name: '憨憨',
  }
}

export default createStore({
  state: {
    countss: 10,
    people:[
      {id: '001',name: '贝贝', age: '10'},
      {id: '002',name: '静静', age: '20'},
      {id: '003',name: '关关', age: '32'},
      {id: '004',name: '悠悠', age: '43'},
      {id: '005',name: '妮妮', age: '12'},
      {id: '006',name: '缓缓', age: '41'},
    ],
    info: {
      name: '原木',
      age: 19
    },
//-项目实操
    loginInfo: {},//存储用户信息
    nickname: localStorage.getItem('nickname')?localStorage.getItem('nickname'):'',
    //存储token
    //Authorization:localStorage.getItem('Authorization')?localStorage.getItem('Authorization'):''
  },
  //---------------------------------------------------------------------------------------
  mutations: {
    //vuex的练习
    countAdd(state) {
      state.countss++;
    },
    countDe(state) {
      state.countss--;
    },
    increatCount(state, count) {
      state.countss+=count;
    },
    updateInfo(state) {
       //Vue.set(state.info, 'address', '洛杉矶')
      //修改姓名是没有问题的
      state.info.name='aaa'
    },
    //getUserNickname(state, nickname) {
    //  state.nickname = nickname;
    //  localStorage.setItem('nickname',nickname);
    //
    //}
  },
//---------------------------------------------------------------------------------------
  getters: {
      pfCount(state) {
        return state.countss * state.countss
      },
    more20age(state) {
        return state.people.filter(item => item.age >20 )
    },
    more20ageLength(state,getter) {
      return getter.more20age.length
    },
    moreAge(state) {
      return function (age) {
        return state.people.filter( item => item.age >age)
      }
    },
  },
//---------------------------------------------------------------------------------------
  actions: {
    aupdateInfo(context, payload) {
      setTimeout(() => {
        context.commit('updateInfo');
        console.log(payload.message);
        payload.success()
      },1000)
    },
    AupdateInfo(context, payload) {
      return new Promise((resolve,reject) => {
      setTimeout(() => {
        context.commit('updateInfo',payload);
        console.log(payload.message);
        resolve('1111')
      },1000)
      })
    }
  },
  modules: {
    a: moduleA
  },
})





























